package com.example.demo.controller;

import com.example.demo.pojo.DetectionOrganizationDto;
import com.example.demo.pojo.Student;
import com.example.demo.util.ExcelBean;
import com.example.demo.util.ExcelUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RequestWrapper;
import java.beans.IntrospectionException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/7/23 21:26
 */
@RequestMapping("/test/student")
@RestController
public class RestFulTestController {

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String getUser(@PathVariable  String id){
        System.out.println(id);
        return id;
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public String getUser(Student student){
        System.out.println(student.toString());
        return student.toString();
    }
    @GetMapping("stack")
   public Stack<Map<String,Object>> getZhan(HttpRequest request){
        Map<String,Object> map=new HashMap<>();
        map.put("name","傅翔1");
        map.put("age",32);
        map.put("sex","男");
        Map<String,Object> map2=new HashMap<>();
        map2.put("name","傅翔2");
        map2.put("age",32);
        map2.put("sex","男");
        Map<String,Object> map3=new HashMap<>();
        map3.put("name","傅翔3");
        map3.put("age",32);
        map3.put("sex","男");
        Stack<Map<String,Object>> stack=new Stack<>();
        stack.push(map);
        stack.push(map2);
        stack.push(map3);
        System.out.println(stack);
        return  stack;
   }
    /**
     * http://localhost:8080/test/student/testa
     */
    @GetMapping("/testa")
    public void testa(HttpServletResponse response) throws IllegalAccessException, IntrospectionException, InvocationTargetException {

        List<DetectionOrganizationDto> list=new ArrayList<>();
        Map<Integer, List<ExcelBean>> mapEX = new LinkedHashMap<>();
        List<ExcelBean> ems = new ArrayList<>();
      /*  ems.add(new ExcelBean("基因检测机构", "supplierName", 0));
        ems.add(new ExcelBean("账号", "superUserName", 0));
        ems.add(new ExcelBean("密码", "password", 0));
        ems.add(new ExcelBean("联系人", "consigneeName", 0));
        ems.add(new ExcelBean("手机号", "consigneePhone", 0));
        ems.add(new ExcelBean("省", "provinceId", 0));
        ems.add(new ExcelBean("收货地址", "consignAddress", 0));*/

        mapEX.put(0, ems);
        //XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFWorkbook sheet1 = ExcelUtils.createExcelFile(DetectionOrganizationDto.class, list, mapEX, "sheet1",0);
        //响应到客户端
        try {
            this.setResponseHeader(response, "c测试.xlsx");
            OutputStream os = response.getOutputStream();
            sheet1.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
